package quickstart.component

import groovy.transform.CompileStatic
import javafx.event.Event
import javafx.fxml.FXML
import javafx.scene.Node
import javafx.scene.control.TextArea
import org.jacpfx.api.annotations.Resource
import org.jacpfx.api.annotations.component.DeclarativeView
import org.jacpfx.api.message.Message
import org.jacpfx.rcp.component.FXComponent
import org.jacpfx.rcp.context.Context
import org.jacpfx.rcp.util.FXUtil

import java.util.logging.Logger

import static quickstart.config.BasicConfig.COMPONENT_LEFT
import static quickstart.config.BasicConfig.COMPONENT_RIGHT
import static quickstart.config.BasicConfig.TARGET_CONTAINER_MAIN

/**
 * Created by TK-chenbin on 2015/5/20.
 */
@CompileStatic
@DeclarativeView(id = COMPONENT_RIGHT,
        name = "SimpleView",
        viewLocation = "/fxml/ComponentRight.fxml",
        active = true,
        resourceBundleLocation = "bundles.languageBundle",
        initialTargetLayoutId = TARGET_CONTAINER_MAIN)
class ComponentRight implements FXComponent {
    private Logger log = Logger.getLogger(getClass().name)

    @Resource
    private Context context

    @FXML
    private TextArea name

    @Override
    Node handle(final Message<Event, Object> message) throws Exception {
        return null
    }

    @Override
    Node postHandle(final Node node, final Message<Event, Object> message) throws Exception {
        if (!message.messageBodyEquals(FXUtil.MessageUtil.INIT)) {
            name.text = message.getTypedMessageBody(String)
        }
        return null
    }

    @FXML
    void sayHello() {
        context.send(COMPONENT_LEFT, "hello")
    }
}
